<template>
  <div class="my_container">
    <div>
      <!-- 未登录头部 -->
      <div v-if="!user" class="header login_out">
        <div class="login_register" @click="$router.push('/login')">
          <img class="mobile-img" src="~@/assets/mobile.png" />
          <span class="text">登录/注册</span>
        </div>
      </div>
      <!-- 已登录头部 -->
      <div v-else class="header login_userInfos">
        <div class="base_info">
          <div class="left">
            <van-image
              round
              fit="cover"
              class="my_img"
              :src="userInfos.photo"
            />
            <span class="my_name">{{ userInfos.name }}</span>
          </div>
          <div class="right">
            <van-button size="mini" round to="/user/profile"
              >编辑资料</van-button
            >
          </div>
        </div>
        <div class="data_status">
          <div class="data_item">
            <div class="count">{{ userInfos.art_count }}</div>
            <div class="text">头条</div>
          </div>
          <div class="data_item">
            <div class="count">{{ userInfos.follow_count }}</div>
            <div class="text">关注</div>
          </div>
          <div class="data_item">
            <div class="count">{{ userInfos.fans_count }}</div>
            <div class="text">粉丝</div>
          </div>
          <div class="data_item">
            <div class="count">{{ userInfos.like_count }}</div>
            <div class="text">获赞</div>
          </div>
        </div>
      </div>
      <!-- 宫格导航区域 -->
      <van-grid :column-num="2" class="grid_box" clickable>
        <van-grid-item class="grid_item">
          <i slot="icon" class="xcc xccshoucang"></i>
          <span slot="text" class="text">收藏</span>
        </van-grid-item>
        <van-grid-item class="grid_item">
          <i slot="icon" class="xcc xcclishi"></i>
          <span slot="text" class="text">历史</span>
        </van-grid-item>
      </van-grid>
      <van-cell title="消息通知" is-link />
      <van-cell title="小熊同学" is-link />
      <!-- 退出 -->
      <van-cell
        clickable
        v-if="user"
        @click="loginOutBtn"
        class="loginOut"
        title="退出登录"
        url
      />
      <!-- 退出 -->
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfos } from '@/api/user.js'
export default {
  name: 'myInfos',
  data() {
    return {
      userInfos: {} // 用户信息
    }
  },
  created() {
    // 初始化的时候 如果用户登录了 显示已登录用户信息
    if (this.user) {
      this.loadingStatus()
    }
  },
  methods: {
    loginOutBtn() {
      this.$dialog
        .confirm({
          title: '退出登录',
          message: '是否确认退出？'
        })
        .then(() => {
          // on confirm
          this.$store.commit('setUser', null)
        })
        .catch(() => {
          // on cancel
          this.$toast.fail('已取消')
        })
    },
    async loadingStatus() {
      try {
        const res = await getUserInfos()
        this.userInfos = res.data.data
      } catch (err) {
        this.$toast('获取数据失败，稍后再试')
      }
    }
  },
  computed: {
    ...mapState(['user'])
  }
}
</script>

<style scoped lang="less">
.my_container {
  .login_out {
    height: 361px;
    background: url('~@/assets/banner.png') no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    .mobile-img {
      width: 132px;
      height: 132px;
      margin-bottom: 10px;
    }
    .text {
      font-size: 28px;
      color: #fff;
    }
    .login_register {
      display: flex;
      justify-content: center;
      align-items: center;
      flex-direction: column;
    }
  }
  .loginOut {
    text-align: center;
    margin-top: 15px;
    color: #eb5253;
  }
  .login_userInfos {
    background: url('~@/assets/banner.png') no-repeat;
    .base_info {
      height: 231px;
      padding: 76px 32px 23px;
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .data_status {
      display: flex;
      .data_item {
        height: 130px;
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: center;
        flex-direction: column;
        color: #fff;
        .count {
          font-size: 36px;
        }
        .text {
          font-size: 23px;
        }
      }
    }
    .left {
      display: flex;
      align-items: center;
      .my_img {
        width: 132px;
        height: 132px;
        margin-right: 23px;
        border: 4px solid #fff;
      }
      .my_name {
        font-size: 30px;
        color: #fff;
      }
    }
  }
  .grid_box {
    .grid_item {
      height: 141px;
      i.xcc {
        font-size: 45px;
      }
      span.text {
        font-size: 28px;
      }
      .xccshoucang {
        color: #eb5253;
      }
      .xcclishi {
        color: #ff9d1d;
      }
    }
  }
}
</style>
